package org.example.coupon.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.example.core.utils.PageUtils;
import org.example.core.utils.R;
import org.example.coupon.dao.SeckillSessionDao;
import org.example.coupon.entity.SeckillSessionEntity;
import org.example.coupon.service.SeckillSkuRelationService;
import org.example.coupon.vo.SRelationVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.example.coupon.service.SeckillSessionService;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;


@Service("seckillSessionService")
public class SeckillSessionServiceImpl implements SeckillSessionService {

    @Autowired
    private SeckillSessionDao seckillSessionDao;
    @Autowired
    private SeckillSkuRelationService seckillSkuRelationService;

    @Override
    public R<?> addSeckillSession(SeckillSessionEntity seckillSession) {
        if(seckillSession.getEndTime().isBefore(seckillSession.getStartTime())){
            return R.error("时间设置不合理");
        }
        seckillSession.setCreateTime(LocalDateTime.now());
        int isOK = seckillSessionDao.saveSeckillSecssion(seckillSession);
        if (isOK > 0){
            return R.ok();
        }
        return R.error();
    }

    @Override
    public PageUtils querySeckillSessionList(Integer pageNum, Integer pageSize, String name, Integer status) {
        PageHelper.startPage(pageNum, pageSize);
        List<SeckillSessionEntity> entityList = seckillSessionDao.getSeckillSessionList(name, status);
        return new PageUtils(new PageInfo<>(entityList));
    }

    @Override
    public SeckillSessionEntity querySeckillSessionInfo(Long id) {
        return seckillSessionDao.getSeckillSessionInfo(id);
    }

    @Override
    public R<?> updateSeckillSession(SeckillSessionEntity seckillSession) {
        if(seckillSession.getEndTime().isBefore(seckillSession.getStartTime())){
            return R.error("时间设置不合理");
        }
        int isOK = seckillSessionDao.updateSeckillSecssion(seckillSession);
        if (isOK > 0){
            return R.ok();
        }
        return R.error();

    }

    @Override
    public List<SRelationVo> querySelectSeckillSession(Long promotionId) {
        List<SeckillSessionEntity> entityList = seckillSessionDao.getStatusZeroSessionList(0);
        List<SRelationVo> sRelationVos = null;
        if (entityList != null && !entityList.isEmpty()){
            sRelationVos = entityList.stream().map(item -> {
                SRelationVo relationVo = new SRelationVo();
                BeanUtils.copyProperties(item, relationVo);
                relationVo.setCount(seckillSkuRelationService.getCount(promotionId, item.getId()));
                return relationVo;
            }).collect(Collectors.toList());
        }
        return sRelationVos;
    }

    @Override
    public List<SeckillSessionEntity> canSession() {
        return seckillSessionDao.getSeckillSessionList(null, 0);
    }
}